package com.atguigu.gulimall.search.utils;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @NAME: CreatEs
 * @USER: wjj
 * @DATE: 2023/11/20
 * @TIME: 17:03
 * @DAY_NAME_FULL: 星期一
 * @PROJECT_NAME: gulimall
 */
public class CreatEs {
    public void creatEs(String eUrl, String pUrl){
        RestTemplate restTemplate=new RestTemplate();//redis提供发送请求
        HttpHeaders headers1 = new HttpHeaders();
        headers1.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);
        String ss="{\n" +
                "  \"mappings\": {\n" +
                "    \"properties\": {\n" +
                "      \"skuId\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"spuId\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"skuTitle\": {\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_smart\"\n" +
                "      },\n" +
                "      \"skuPrice\": {\n" +
                "        \"type\": \"float\"\n" +
                "      },\n" +
                "      \"skuImg\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"saleCount\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"hosStock\": {\n" +
                "        \"type\": \"boolean\"\n" +
                "      },\n" +
                "      \"hotScore\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"brandId\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"catelogId\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"brandName\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"brandImg\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"catalogName\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"attrs\": {\n" +
                "        \"type\": \"nested\",\n" +
                "        \"properties\": {\n" +
                "          \"attrId\": {\n" +
                "            \"type\": \"long\"\n" +
                "          },\n" +
                "          \"attrName\": {\n" +
                "            \"type\": \"keyword\"\n" +
                "          },\n" +
                "          \"attrValue\": {\n" +
                "            \"type\": \"keyword\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        HttpEntity<String> entity = new HttpEntity<>(ss, headers1);

        try {

                restTemplate.put("http://"+eUrl+":9200/gulimall_product",entity);

        } catch (RestClientException e) {
            System.out.println("上架失败。原因");
            e.printStackTrace();
        } finally {
            /**
             * 重新上架
             */


            // 发送JSON格式的POST请求
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            String json = "{}";
            HttpEntity<String> request1 = new HttpEntity<>(json, headers);
            restTemplate.postForEntity("http://"+pUrl+"/product/spuinfo/2/up", request1, String.class);
            restTemplate.postForEntity("http://"+pUrl+"/product/spuinfo/3/up", request1, String.class);
            restTemplate.postForEntity("http://"+pUrl+"/product/spuinfo/4/up", request1, String.class);
        }


    }
}
